package view;

import model.User;
import controller.UserController;

import java.util.Scanner;

public class UserView {
    private static UserController userController = new UserController();
    private static Scanner scanner = new Scanner(System.in);

    public static void displayMenu() {
        while (true) {
            System.out.println("1. 添加用户");
            System.out.println("2. 查询用户");
            System.out.println("3. 更新用户");
            System.out.println("4. 删除用户");
            System.out.println("5. 列出所有用户");
            System.out.println("6. 退出");
            System.out.print("Choose an option: ");

            int option = scanner.nextInt();
            scanner.nextLine();  // Consume newline

            switch (option) {
                case 1:
                    addUser();
                    break;
                case 2:
                    queryUser();
                    break;
                case 3:
                    updateUser();
                    break;
                case 4:
                    deleteUser();
                    break;
                case 5:
                    listAllUsers();
                    break;
                case 6:
                    return;
                default:
                    System.out.println("Invalid option. Please try again.");
            }
        }
    }

    private static void addUser() {
        User user = new User();
        System.out.print("Enter username: ");
        user.setUsername(scanner.nextLine());
        System.out.print("Enter password: ");
        user.setPassword(scanner.nextLine());
        System.out.print("Enter role (Admin/User): ");
        user.setRole(scanner.nextLine());
        userController.addUser(user);
        System.out.println("User added.");
    }

    private static void queryUser() {
        System.out.print("Enter username to query: ");
        User user = userController.getUser(scanner.nextLine());
        if (user != null) {
            System.out.println("Username: " + user.getUsername() + ", Role: " + user.getRole());
        } else {
            System.out.println("User not found.");
        }
    }

    private static void updateUser() {
        User user = new User();
        System.out.print("Enter username to update: ");
        user.setUsername(scanner.nextLine());
        System.out.print("Enter new password: ");
        user.setPassword(scanner.nextLine());
        System.out.print("Enter new role (Admin/User): ");
        user.setRole(scanner.nextLine());
        userController.updateUser(user);
        System.out.println("User updated.");
    }

    private static void deleteUser() {
        System.out.print("Enter username to delete: ");
        userController.deleteUser(scanner.nextLine());
        System.out.println("User deleted.");
    }

    private static void listAllUsers() {
        for (User user : userController.getAllUsers()) {
            System.out.println("Username: " + user.getUsername() + ", Role: " + user.getRole());
        }
    }

}
